
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:请求论坛接口
 *
 * @author jerryliu
 * @since 2015年11月19日 上午9:37:25
 */
public class GameForumTask {
    private ArrayList<String> mCmdList = new ArrayList<String>();

    public AppInfoHttpResponse request() {
        try {
            ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
            HttpBaseRequestPackage req = new HttpBaseRequestPackage();
            ResponsePackage responsePackage = new HttpResponsePackage();
            AppInfoHttpResponse appsHttpResponse = new AppInfoHttpResponse();
            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("cmd", ProtocolCmd.CMD_REQUEST_FORUM_URL);
            requestParams.add(params);
            req.setRequestParams(requestParams);
            mCmdList.add(ProtocolCmd.CMD_REQUEST_FORUM_URL);
            KGHttpClient.request(req, responsePackage);
            responsePackage.getResponseData(appsHttpResponse);
            return appsHttpResponse;
        } catch (AppException e) {
            e.printStackTrace();
        }
        return null;
    }

    private class HttpResponsePackage extends HttpBaseResponsePackage<AppInfoHttpResponse> {

        @Override
        protected void handleResponse(AppInfoHttpResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "GameForumTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.optJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    // 根据cmd解析每个请求
                    if (ProtocolCmd.CMD_REQUEST_FORUM_URL.equalsIgnoreCase(cmd)) {
                        JSONObject jsonResult = jsonResp.optJSONObject("data");
                        if (jsonResult != null) {
                            response.setForumUrl(jsonResult.optString("bbsurlonmobile"));
                        }
                    }
                    response.setOk(true);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public class AppInfoHttpResponse extends ResponseMessage {
        private static final long serialVersionUID = -589531768091264866L;

        private String forumUrl;

        public String getForumUrl() {
            return forumUrl;
        }

        public void setForumUrl(String forumUrl) {
            this.forumUrl = forumUrl;
        }

    }
}
